/* header 购物车显示 */
let uid1 = localStorage.getItem('uid');
if (uid1 != null) {
    axios.get(`http://localhost:3000/carts/?uid=${uid1}`)
        .then(function (res) {
            let length = `(${res.data.length})`
            $('#header #right b').html(length);
        })
}



//下拉菜单
var html = '';
$('#nav ul li').mouseenter(function () {
    if(this.id == 0 || this.id == 4 || this.id ==5){
        $('#nav div').css('display','none');
    }
    if (this.id == 1) {
        $('#nav div').css('display', 'block');
        let url1 = 'http://localhost:3000/pn1';
        ajax(url1);
    }
    if (this.id == 2) {
        $('#nav div').css('display', 'block');
        let url2 = 'http://localhost:3000/pn2';
        ajax(url2);
    }
    if (this.id == 3) {
        $('#nav div').css('display', 'block');
        let url3 = 'http://localhost:3000/pn3';
        ajax(url3);
    }
})
$('#nav').mouseleave(function () {
    $('#nav div').css('display', 'none');
})
function ajax(url){
    axios.get(url)
            .then(function (res) {
                html = '';
                for (let i = 0; i < 6; i++) {
                    html += `<li pid="${res.data[i].id}" pclass="${res.data[i].pclass}">
                <img src="${res.data[i].img[0]}" alt="">
                <h2>${res.data[i].title}</h2>
                <span>${res.data[i].price}</span>
            </li>`
                }
                $("#nav div ul").html(html)
            })
            .catch(function (err) {
                console.log(err)
            })
}

/* click事件 */
$('#nav').on('click', 'li', function () {
    if (this.id == 1) {
        localStorage.setItem('pclass',"pn1")
    }
    if (this.id == 2) {
        localStorage.setItem('pclass',"pn2")
    }
    if (this.id == 3) {
        localStorage.setItem('pclass',"pn3")
    }
})